package com.yun.blog.controller;

import com.yun.blog.bean.Blog;
import com.yun.blog.bean.Category;
import com.yun.blog.bean.Result;
import com.yun.blog.bean.Tag;
import com.yun.blog.bean.dto.DataDto;
import com.yun.blog.service.BlogService;
import com.yun.blog.service.CategoryService;
import com.yun.blog.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * title        : IndexController
 * projectName  : dantouBlog-back
 * description  : TODO
 * author       : simple
 * email        : yun_simple@163.com
 * date         : 2023/1/13下午6:22
 */

@Slf4j
@RestController
@RequestMapping("/index")
public class IndexController {

    @Resource
    BlogService blogService;

    @Resource
    CategoryService categoryService;
    @Resource
    TagService tagService;


    @GetMapping("/getShowData")
    public Result getShowData() {

        Result success = Result.success();
        // 首页首行四个数据
        success.add("articleCount", blogService.count());
        success.add("viewCount", blogService.getViewCount());
        success.add("categoryCount", categoryService.count());
        success.add("tagCount", tagService.count());

        // 分类统计
        ArrayList<DataDto> categoryPieData = new ArrayList<>();
        List<Category> categories = categoryService.getByIsUse(true);
        for (Category category : categories) {
            if (category.getBlogCount() == null || category.getBlogCount() == 0) {
                continue;
            }
            DataDto dataDto = new DataDto();
            dataDto.setName(category.getCategoryName());
            // dataDto.setValue(blogService.getCountByCategory(category));
            dataDto.setValue(category.getBlogCount());
            categoryPieData.add(dataDto);
        }
        success.add("categoryPieData", categoryPieData);

        // 标签统计
        ArrayList<DataDto> tagPieData = new ArrayList<>();
        List<Tag> tags = tagService.listByIsUse(true);
        for (Tag tag : tags) {
            if (tag.getBlogCount() == null || tag.getBlogCount() == 0) {
                continue;
            }
            DataDto dataDto = new DataDto();
            dataDto.setName(tag.getTagName());
            dataDto.setValue(tag.getBlogCount());
            tagPieData.add(dataDto);
        }
        success.add("tagPieData", tagPieData);

        // 浏览量排行前十
        List<Blog> viewsRankTop10 = blogService.getViewRank(10);
        success.add("viewsRankTop10", viewsRankTop10);

        return success;
    }

}
